package ex;

import java.util.TreeSet;

/**
 * @author: knowno
 * @create: 2025-09-16 08:59
 * @Version 1.0
 **/
public class TestTreeSet2 {
    public static void main(String[] args) {

        TreeSet<Object> objects = new TreeSet<>();
        Person person = new Person("b",28);
        objects.add(person);
        Person person1 = new Person("a",19);
        objects.add(person1);
        Person person2 = new Person("c",20);
        objects.add(person2);
        //添加元素的时候,随意插入的,实际会按照"大小"(自然顺序)顺序排序

        System.out.println(objects);



    }
}

//java.lang.Comparable
//public interface Comparable<T> {
//    public int compareTo(T o);
//}
class  Person implements Comparable<Person>{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Person o) {
        //return this.age-o.age;
        //return this.name.compareTo(o.name);
        return  o.name.compareTo(this.name);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
